package com.gin.no_gay_alliance.fragment.daily;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.exception.NgaException;
import com.gin.nga.response.body.nuke.FollowStatusBody;
import com.gin.no_gay_alliance.adpter.follow.DynamicsAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.callback.Callback;
import com.gin.no_gay_alliance.databinding.FragmentDailyDynamicsBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.listener.MyScrollListener;

import java.io.IOException;

import okhttp3.Call;

/**
 * 关注动态
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/13 09:56
 **/
public class DynamicsFragment extends BaseBindingFragment<FragmentDailyDynamicsBinding> implements MyScrollListener.OnScrollToBottomListener, Callback<FollowStatusBody>, SwipeRefreshLayout.OnRefreshListener {
    int page = 1;
    boolean hasNext = true;
    private DynamicsAdapter adapter;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        Log.d(TAG, "onCreateView: " + "动态");

        page = 1;
        hasNext = true;
        adapter = new DynamicsAdapter(requireContext());
        MyScrollListener listener = new MyScrollListener();
        listener.setOnScrollToBottomListener(this);
        b.recyclerView.addOnScrollListener(listener);
        b.recyclerView.setAdapter(adapter);

        b.swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        sendRequest();
    }

    /**
     * 已滚动到底部
     */
    @Override
    public void onScrollToBottom() {
        sendRequest();
    }


    /**
     * 请求成功回调
     *
     * @param data 数据
     */
    @Override
    public void onSuccess(FollowStatusBody data) {
        if (data == null || ObjectUtils.isEmpty(data.getData())) {
            hasNext = false;
            return;
        }
        page++;
        ui(() -> adapter.pushData(data.getData().values()));
        onRequestFinally();
    }

    /**
     * 请求之前执行的操作
     */
    @Override
    public void beforeRequest() {
        toast("正在加载更多数据..");
        ui(() -> b.swipeRefreshLayout.setRefreshing(true));
    }

    /**
     * 请求失败回调(一般为网络访问错误)
     *
     * @param call call
     * @param e    错误
     */
    @Override
    public void onFailure(@NonNull Call call, @NonNull IOException e) {
        onRequestFinally();
        Callback.super.onFailure(call, e);
    }

    /**
     * 处理Nga异常
     *
     * @param e 异常
     */
    @Override
    public void handleException(NgaException e) {
        onRequestFinally();
        Callback.super.handleException(e);
    }

    /**
     * 手动刷新
     */
    @Override
    @SuppressLint("NotifyDataSetChanged")
    public void onRefresh() {
        page = 1;
        hasNext = true;
        adapter.clear();
        adapter.notifyDataSetChanged();
        sendRequest();
    }

    private void onRequestFinally() {
        ui(() -> b.swipeRefreshLayout.setRefreshing(false));
    }

    private void sendRequest() {
        if (!hasNext) {
            toast("没有更多数据");
            return;
        }
        MyApplication.app.managerFactory.getFollowManager().followStatus(page, this);
    }

}
